﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace Web4Comment.Service
{
    //public class UserService:IUserService
    public static class UserService
    {
        private static UnlessComment.BLL.User userBLL = new UnlessComment.BLL.User();
        private static UnlessComment.BLL.UserInfo userInfoBLL = new UnlessComment.BLL.UserInfo();

        public static int register(UnlessComment.Model.User user, UnlessComment.Model.UserInfo userInfo)
        {
            int exist = 0;

            if (userBLL.GetModelList(" username ='" + user.username + "'").Count > 0)
                exist = exist - 1;

            if (userBLL.GetModelList(" email ='" + user.email + "'").Count > 0)
                exist = exist - 2;

            if (exist != 0)
                return exist;

            
            user.is_active = true;
            user.date_join = DateTime.Now;
            int id = userBLL.Add(user);
            if (id != 0)
            {
                userInfo.user_id = id;
                if (userInfoBLL.Add(userInfo))
                {
                    return id;
                }
                else
                {
                    return 0;
                }
            }
            else
                return 0;
        }

        public static int update(UnlessComment.Model.User user, UnlessComment.Model.UserInfo userInfo)
        {
            int user_id = getUserid(user.username);
            if (user_id > 0)
            {
                user.id = user_id;
                if (!userBLL.Update(user))
                    return 0;
                if (!userInfoBLL.Update(userInfo))
                    return 0;

                return 1;

            }
            else
            {
                return 0;
            }
        }

        public static int logon(string username, string password)
        {
            UnlessComment.Model.User userModel = new UnlessComment.Model.User();
            DataSet ds = userBLL.GetList(" username ='" + username + "' and password ='" + password + "'");
            if(ds.Tables[0].Rows.Count > 0)
            {
                userModel = userBLL.DataTableToList(ds.Tables[0])[0];
            }
            else
            {
                return 0;
            }
            int id = userModel.id;
            userModel.last_login = DateTime.Now;
            userBLL.Update(userModel);
            return id;
        }

        public static int edit(UnlessComment.Model.User user, UnlessComment.Model.UserInfo userInfo)
        {
            
            return 0;
        }

        public static int existUsername(string username)
        {
            return userBLL.GetModelList(" username ='" + username + "'").Count;
        }

        public static int existEmail(string email)
        {
            return userBLL.GetModelList(" email ='" + email + "'").Count;
        }

        public static int getUserid(string username)
        {
            List<UnlessComment.Model.User> users = new List<UnlessComment.Model.User>();
            users = userBLL.GetModelList(" username ='" + username + "'");
            if (users.Count == 1)
                return users[0].id;
            else
                return 0;
        }


        public static UnlessComment.Model.UserTotal getUserTotal(string username)
        {
            UnlessComment.Model.UserTotal userTotal = new UnlessComment.Model.UserTotal();

            int user_id = getUserid(username);
            userTotal.user = userBLL.GetModel(user_id);
            userTotal.userInfo = userInfoBLL.GetModel(user_id);

            return userTotal;
        }
    
    }
}